package com.woniu.park.service.impl;

import com.woniu.park.controller.form.TransferInfoForm;
import com.woniu.park.dao.OrderMapper;
import com.woniu.park.dao.ParkspaceMapper;
import com.woniu.park.dao.UserMapper;
import com.woniu.park.entity.Order;
import com.woniu.park.entity.Parkspace;
import com.woniu.park.entity.TransferInfo;
import com.woniu.park.dao.TransferInfoMapper;
import com.woniu.park.entity.User;
import com.woniu.park.service.ITransferInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.park.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxb
 * @since 2020-09-08
 */
@Service
public class TransferInfoServiceImpl extends ServiceImpl<TransferInfoMapper, TransferInfo> implements ITransferInfoService {
    @Autowired
    private TransferInfoMapper transferInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ParkspaceMapper parkspaceMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public int insertTransfer(int userId, double money,int orderId) {

        TransferInfo transferInfo = new TransferInfo();
        transferInfo.setTransferInfoMoney(money);
        transferInfo.setTransferInfoUserId(userId);
        if (orderId!=0) {
            transferInfo.setTransferOrderId(orderId);
        }
        //默认未支付
        transferInfo.setTransferInfoStatus(0);
        transferInfo.setTransferInfoTime(new Date());
        transferInfoMapper.insert(transferInfo);
        return transferInfo.getTransferInfoId();
    }

    @Transactional
    public void updateTransfer(TransferInfoForm transferInfoForm) {
        //(原参数) int id, String trade_no, String subject,int userId,double money
        TransferInfo transferInfo = new TransferInfo();
        //沙箱回调完成修改状态为已支付
        transferInfo.setTransferInfoStatus(1);
        transferInfo.setTradeNo(transferInfoForm.getTradeNo());
        transferInfo.setTransferOrderId(transferInfoForm.getTransferOrderId());
        transferInfo.setTransferInfoId(transferInfoForm.getTransferInfoId());
        transferInfoMapper.updateById(transferInfo);
        if (transferInfoForm.getSubject().equals(Status.PAY_PAYMONRY)) {
            //修改订单状态
            Order order = new Order();
//            order.setOrderUserId(transferInfoForm.getUserId());
//            order.setOrderPrice(transferInfoForm.getTransferInfoMoney());
            order.setOrderStatus(Status.ORDER_STATUS2);
            order.setOrderParkspaceStatus(Status.ORDER_STATUS2);
            order.setOrderId(transferInfoForm.getTransferOrderId());
            /*order.setOrderParkspaceId(transferInfoForm.getParkspaceId());*/
            /*    //根据车位id查询相关信息
            Parkspace parkspace = parkspaceMapper.selectById(transferInfoForm.getParkspaceId());
            order.setOrderParkspaceNum(parkspace.getParkspaceNum());
            order.setOrderParkAddress(parkspace.getParkspaceAddress());
            order.setOrderDay(transferInfoForm.getStoppingTime());*/
            orderMapper.updateById(order);


        }
        if (transferInfoForm.getSubject().equals(Status.PAY_ADDMONRY)) {
            //修改用户余额
            User user = userMapper.selectById(transferInfoForm.getTransferInfoUserId());
            user.setUserBalance(user.getUserBalance()+transferInfoForm.getTransferInfoMoney());
            userMapper.updateById(user);
        }
    }
}
